<?php

if (!defined("PUBLIC_HTML"))
{

    exit;

}

/**
 * FileManager
 * 
 * @package   
 * @author CMS
 * @copyright Christopher Hazekamp
 * @version 2010
 * @access public
 */
class FileManager
{

    /**
     * FileManager::upload()
     * 
     * @param mixed $input_name
     * @param mixed $folder
     * @param string $file_types
     * @param string $file_max_size
     * @param string $default_return
     * @return
     */
    public function upload($input_name, $folder, $file_types='', $file_max_size='100000000', $default_return='')
	{
		global $_FILES;
		$path       = $folder.$_FILES[$input_name]['name'];
		$filesize   = $_FILES[$input_name]['size'];
		
		if (!$filesize == 0)
		{
			if (is_numeric($filesize) && $filesize < $file_max_size)
			{
				if (is_array($file_types))
				{
					foreach ($file_types as $key)
					{
						if($_FILES[$input_name]['type'] == $key)
						{
							$allow_upload = 1;
							unset($file_types);
						}
						else
						{
							$allow_upload = 0;
						}
					}
				}
				elseif (!$file_types == '')
				{
					if ($_FILES[$input_name]['type'] == $file_types)
					{
						$allow_upload = 1;
						unset($file_types);
					}
					else
					{
						$allow_upload = 0;
					}
				}
				else
				{
					$allow_upload = 1;
				}
			}
			elseif ($filesize > $file_max_size)
			{
				$allow_upload = 0;
			}
			else
			{
				if (is_array($file_types))
				{
					foreach ($file_types as $key)
					{
						if($_FILES[$input_name]['type'] == $key)
						{
							$allow_upload = 1;
							unset($file_types);
						}
						else
						{
							$allow_upload = 0;
						}
					}
				}
				elseif (!$file_types == '')
				{
					if ($_FILES[$input_name]['type'] == $file_types)
					{
						$allow_upload = 1;
						unset($file_types);
					}
					else
					{
						$allow_upload = 0;
					}
				}
				else
				{
					$allow_upload = 1;
				}
			}
		}
		else
		{
			$allow_upload = 0;
		}
		
		if ($allow_upload == 1)
		{
			copy($_FILES[$input_name]['tmp_name'], $path);
		}
		$new_file = ($allow_upload == 1) ? $_FILES[$input_name] : '';
		return $new_file;
	}
	
	/**
	 * FileManager::create()
	 * 
	 * @param mixed $file
	 * @param mixed $new_value
	 * @return
	 */
	public function create($file, $new_value)
	{
		if (!file_exists($file))
		{
			$new_file = fopen($file, 'w');
			fwrite($new_file, $new_value);
			fclose($new_file);
			$return = true;
		}
		else
		{
			$return = false;
		}
	}
	
	/**
	 * FileManager::edit()
	 * 
	 * @param mixed $file
	 * @param mixed $new_value
	 * @return
	 */
	public function edit($file, $new_value)
	{
		if (file_exists($file))
		{
			if (is_readable($file))
			{
				if (is_writeable($file))
				{
					$file_open	= fopen($file, 'w');
					$file_edit	= (fwrite($file_open, $new_value)) ? true : false;
					$return		= $file_edit;
				}
			}
		}
		
		return true;
	}
	
	/**
	 * FileManager::delete()
	 * 
	 * @param mixed $file
	 * @return
	 */
	public function delete($file)
	{
		if (file_exists($file) && is_readable($file_exists))
		{
			@unlink($file);
			$return = true;
		}
		else
		{
			$return = false;
		}
		
		return $return;
	}
	
	/**
	 * FileManager::content()
	 * 
	 * @param mixed $file
	 * @param string $offset
	 * @param string $limit
	 * @return
	 */
	public function content($file, $offset='0', $limit='')
	{
		$offset = (is_numeric($offset)) ? $offset	: '0';
		if (file_exists($file))
		{
			if (is_readable($file))
			{
				$get_file_content = (is_numeric($limit)) ? file_get_contents($file, '', NULL, $offset, $limit) : file_get_contents($file, '', NULL, $offset);
				$return = $get_file_content;
			}
			else
			{
				$msg = 'Bestand kan niet worden gelezen';
				$return = false;
			}
		}
		else
		{
			$msg = 'bestand kan niet worden gevonden';
			$return = false;
		}
		
		return $return;
	}
	
	/**
	 * FileManager::create_dir()
	 * 
	 * @param mixed $new_dir
	 * @param string $chmod
	 * @return
	 */
	public function create_dir($new_dir, $chmod='0700')
	{
		$new_chmod	= (preg_match('/^[0-9]{4}&/', $chmod)) ? $chmod : '0700';
		$create_dir	= mkdir($new_dir, $new_chmod);
		$return		= $create_dir;
		return $return;
	}

	/**
	 * FileManager::edit_dir()
	 * 
	 * @return
	 */
	public function edit_dir()
	{
		//
	}
	
	/**
	 * FileManager::delete_dir()
	 * 
	 * @param mixed $dir
	 * @return
	 */
	public function delete_dir($dir)
	{
		if (file_exists($dir) && !is_file($dir))
		{
			$delete_dir = (rmdir($dir)) ? true : false;
		}
		
		return $delete_dir;
	}
	
	/**
	 * FileManager::scan_dir()
	 * 
	 * @param mixed $dir
	 * @param string $order
	 * @return
	 */
	public function scan_dir($dir, $order='0')
	{
		$dir_to_scan 	= $dir;
		$this->list		= scandir($dir_to_scan, $order);
		return $this->list;
	}
	
	/**
	 * FileManager::rename()
	 * 
	 * @param mixed $folder
	 * @param string $old
	 * @param string $new
	 * @return
	 */
	public function rename($folder, $old, $new)
	{
		rename($folder . $old, $folder . $new);
		return $new;
	}
	
	public function get_extension($file)
	{
		$extensions = explode('.', $file);
		$extension = $extensions[(count($extensions)-1)];
		return $extension;
	}
	
	public function convert_size($value, $oldtype, $newtype)
	{
		switch ($oldtype)
		{
			case 'b':
				switch ($newtype)
				{
					case 'kb':
						$value = '';
						break;
						
					case 'mb':
						$value = '';
						break;
						
					case 'gb':
						$value = '';
						break;
						
					default:
						$value = $value;
						break;
				}
				break;
			
			case 'kb':
				switch ($newtype)
				{
					case 'b':
						$value = '';
						break;
						
					case 'mb':
						$value = '';
						break;
						
					case 'gb':
						$value = '';
						break;
						
					default:
						$value = $value;
						break;
				}
				break;
				
			case 'mb': 
				switch ($newtype)
				{
					case 'kb':
						$value = '';
						break;
						
					case 'b':
						$value = '';
						break;
						
					case 'gb':
						$value = '';
						break;
						
					default:
						$value = $value;
						break;
				}
				break;
				
			case 'gb': 
				switch ($newtype)
				{
					case 'kb':
						$value = '';
						break;
						
					case 'mb':
						$value = '';
						break;
						
					case 'b':
						$value = '';
						break;
						
					default:
						$value = $value;
						break;
				}
				break;
				
			default: 
				$value = $value; 
				break;
		}
		return $value;
	}
}

?>